﻿namespace DesignModeConsole.创建型.简单工厂模式;

/// <summary>
/// 文件读取工厂
/// </summary>
public static class FileReadFactory
{
    public static IReadFile CreateRead(string fileFormat)
    {
        if (fileFormat == "json")
        {
            return new ReadJsonFile();
        }
        else if (fileFormat == "xml")
        {
            return new ReadXmlFile();
        }
        else
        {
            return new ReadTxtFile();
        }
    }
}

/// <summary>
/// 文件读取工厂  第二种方式
/// </summary>
public static class FileReadFactory2
{
    private static readonly Dictionary<string, IReadFile> _files = new Dictionary<string, IReadFile>();

    static FileReadFactory2()
    {
        _files.Add("json", new ReadJsonFile());
        _files.Add("xml", new ReadXmlFile());
        _files.Add("txt", new ReadTxtFile());
    }

    public static IReadFile CreateRead(string fileFormat)
    {
        return _files[fileFormat];
    }
}

/// <summary>
/// 读取文件的接口
/// </summary>
public interface IReadFile
{
    /// <summary>
    /// 读取文件
    /// </summary>
    /// <returns></returns>
    string Read();
}

public class ReadJsonFile : IReadFile
{
    public string Read()
    {
        return "读取json格式文件";
    }
}

public class ReadXmlFile : IReadFile
{
    public string Read()
    {
        return "读取xml格式文件";
    }
}

public class ReadTxtFile : IReadFile
{
    public string Read()
    {
        return "读取txt格式文件";
    }
}